#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->port->setText("8899");
    setWindowTitle("服务器");
    //创建监听的服务器对象
    m_s = new QTcpServer(this);
    connect(m_s,&QTcpServer::newConnection,this,[=](){
        m_tcp = m_s->nextPendingConnection();
        m_status->setPixmap(QPixmap(":/ConnectStatus.png").scaled(20,20));
        //检测是否可以接收数据
        connect(m_tcp,&QTcpSocket::readyRead,this,[=]()
        {
            QByteArray data = m_tcp->readAll();
            ui->record->append("客户端say：" + data);
        });
        //检测客户端是否断开连接
        connect(m_tcp,&QTcpSocket::disconnected,this,[=]()
        {
            m_tcp->close();
            m_tcp->deleteLater();
            //以上两步缺一不可，顺序不可调换
            m_status->setPixmap(QPixmap(":/NoConnectStatus.png").scaled(20,20));
        });
    });
    //状态栏
    m_status = new QLabel;
    m_status->setPixmap(QPixmap(":/NoConnectStatus.png").scaled(20,20));
    ui->statusbar->addWidget(new QLabel("连接状态："));
    ui->statusbar->addWidget(m_status);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_setListen_clicked()
{
    unsigned short port = ui->port->text().toShort();
    m_s->listen(QHostAddress::Any,port);
    ui->setListen->setDisabled(true);
}

void MainWindow::on_sendMsg_clicked()
{
    QString msg = ui->msg->toPlainText();//以纯文本的方式提取出来
    m_tcp->write(msg.toUtf8());
    ui->record->append("服务器say:" + msg);
}
